﻿//公共js
export function showPrompt(message) {
    return prompt(message, 'Type your name here');
}

//https://developer.aliyun.com/article/1263127
export function windowSize() {
    var size = {
        //屏幕大小
        screenWidth: window.screen.width,
        screenHeight: window.screen.height,
        //可用屏幕大小
        availScreenWidth: window.screen.availWidth,
        availScreenHeight: window.screen.availHeight,
        //窗口外部大小
        windowOuterWidth: window.outerWidth,
        windowOuterHeight: window.outerHeight,
        //窗口内部大小
        windowInnerWidth: window.innerWidth,
        windowInnerHeight: window.innerHeight,
        //不带滚动条的窗口内部大小
        windowInnerWidthNoScroll: document.documentElement.clientWidth,
        windowInnerHeightNoScroll: document.documentElement.clientHeight,
        //网页内容的尺寸
        pageWidth: document.documentElement.scrollWidth,
        pageHeight: document.documentElement.scrollHeight,

    };
    return size;
}

// 检查浏览器支持情况
export function checkClipboardSupport() {
    return !!(navigator.clipboard && navigator.permissions);
};

// 设置文本并选中
export function setTextAndSelect(element, text) {
    element.value = text;
    element.select();
    element.setSelectionRange(0, text.length); // 对于移动设备兼容性
};

// 复制文本的通用函数
export function blazorClipboardCopy(text) {
    return navigator.clipboard.writeText(text).then(function () {
        return true;
    }).catch(function (error) {
        console.error('Failed to copy text: ', error);
        return false;
    });
};

export function BlazorFocusElement(element) {
    element.focus();
};

export function fallbackCopyText(id, text) {
    id = id || 'fallbackTextArea';
    const textArea = document.getElementById(id);
    textArea.value = text;
    textArea.select();
    textArea.setSelectionRange(0, text.length); // 对于移动设备兼容性
    // 注意：这里不再尝试 document.execCommand('copy')，因为其兼容性也已下降
    // 改为提示用户手动复制
};